import 'dart:async';

import 'package:flutter/material.dart';
import 'package:untitled/pages/home/tab_search/filter_bar/data.dart';
import 'package:untitled/pages/home/tab_search/filter_bar/item.dart';
import 'package:untitled/scoped_model/room_filter.dart';
import 'package:untitled/utils/common_picker/index.dart';
import 'package:untitled/utils/scopoed_model_helper.dart';

/*
* 找房页头部过滤bar
* */
class FilterBar extends StatefulWidget {
  final ValueChanged<FilterBarResult> onChange;

  const FilterBar({Key key, this.onChange}) : super(key: key);

  @override
  _FilterBarState createState() => _FilterBarState();
}

class _FilterBarState extends State<FilterBar> {
  //标记选中区域
  bool isAreaActive = false; //区域
  bool isRentTypeActive = false; //方式
  bool isPriceActive = false; //租金
  bool isFilterActive = false; //筛选

  //四个选中区域对应四个结果值
  String areaId = '';
  String rentTypeId = '';
  String priceId = '';
  List<String> moreIds = [];

  //四个区域对应四个响应事件
  _onAreaChange(context) {
    setState(() {
      isAreaActive = true;
    });
    var result = CommonPicker.showPicker(
        context: context,
        value: 0, //默认选中索引
        options: areaList.map((e) => e.name).toList());
    result.then((value) {
      if (value == null) return;
      setState(() {
        areaId = areaList[value].id;
      });
      _onChange();
    }).whenComplete(() => isAreaActive = false);
  }

  _onRentTypeChange(context) {
    setState(() {
      isRentTypeActive = true;
    });
    var result = CommonPicker.showPicker(
        context: context,
        value: 0, //默认选中索引
        options: rentTypeList.map((e) => e.name).toList());
    result.then((value) {
      if (value == null) return;
      setState(() {
        areaId = rentTypeList[value].id;
      });
      _onChange();
    }).whenComplete(() => isRentTypeActive = false);
  }

  _onPriceChange(context) {
    setState(() {
      isPriceActive = true;
    });
    var result = CommonPicker.showPicker(
        context: context,
        value: 0, //默认选中索引
        options: priceList.map((e) => e.name).toList());
    result.then((value) {
      if (value == null) return;
      setState(() {
        areaId = priceList[value].id;
      });
      _onChange();
    }).whenComplete(() => isPriceActive = false);
  }

  _onFilterChange(context) {}

  _onChange() {
    var selectedList =ScopedModelHelper.getModel<FilterBarModel>(context).selectedList;
    if (widget.onChange != null) {
      widget.onChange(FilterBarResult(
          areaId: areaId,
          rentTypeId: rentTypeId,
          priceId: priceId,
          moreIds: selectedList.toList()));
    }
  }

  _getData() {
    Map<String,List<GeneralType>> dataList = Map<String,List<GeneralType>>();
    dataList['roomTypeList'] = roomTypeList;
    dataList['orientedList'] = orientedList;
    dataList['floorList'] = floorList;
    ScopedModelHelper.getModel<FilterBarModel>(context).dataList = dataList;
  }

  @override
  void initState() {
    //延迟执行_getData方法,因为在这个生命周期中拿不到Context或拿到的不完整
    Timer.run(_getData);
    super.initState();
  }

  @override
  void didChangeDependencies() {
    _onChange();
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 41,
      decoration: BoxDecoration(
          border: Border(bottom: BorderSide(color: Colors.black12, width: 1))),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          Item(title: '区域', isActive: isAreaActive, onTap: _onAreaChange),
          Item(
            title: '方式',
            isActive: isRentTypeActive,
            onTap: _onRentTypeChange,
          ),
          Item(
            title: '租金',
            isActive: isPriceActive,
            onTap: _onPriceChange,
          ),
          Item(
            title: '筛选',
            isActive: isFilterActive,
            onTap: _onFilterChange,
          ),
        ],
      ),
    );
  }
}
